package com.guli.sms.controller;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;

import com.guli.sms.vo.SeckillSkuRelationWithNamesVo;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.guli.common.pojo.SeckillSkuRelationEntity;
import com.guli.sms.service.SeckillSkuRelationService;

import com.guli.common.utils.R;


/**
 * 秒杀活动商品关联
 *
 * @author LuoJunHua
 * @email 1007052116@qq.com
 * @date 2020-11-29 10:59:49
 */
@RestController
@RequestMapping("/sms/seckillskurelation")
public class SeckillSkuRelationController {

    @Autowired
    private SeckillSkuRelationService seckillSkuRelationService;


    @GetMapping("/list/{currentPage}/{size}")
    @ApiOperation("根据当前页和每页显示的记录数来获取seckillSkuRelation分页列表")
    public R list(@PathVariable("currentPage") Integer currentPage, @PathVariable("size") Integer size, @RequestParam(value = "sessionId", required = false) Long sessionId) throws ExecutionException, InterruptedException {

        Page<SeckillSkuRelationWithNamesVo> page = seckillSkuRelationService.queryPage(currentPage, size, sessionId);

        return R.ok().put("page", page);
    }


    @GetMapping("/info/{id}")
    @ApiOperation("根据id获取SeckillSkuRelation")
    public R info(@PathVariable("id") Long id) {

        SeckillSkuRelationEntity seckillSkuRelation = seckillSkuRelationService.getById(id);

        return R.ok().put("seckillSkuRelation", seckillSkuRelation);
    }

    @PostMapping("/save")
    @ApiOperation("插入SeckillSkuRelation")
    public R save(@RequestBody SeckillSkuRelationEntity seckillSkuRelation) {

        seckillSkuRelationService.saveBatchAndCheckIfRedisNeedesToRefresh(Arrays.asList(seckillSkuRelation));

        return R.ok();
    }

    @PostMapping("/saveBatch")
    @ApiOperation("批量插入SeckillSkuRelation")
    @Transactional
    public R saveBatch(@RequestBody List<SeckillSkuRelationEntity> seckillSkuRelationList) {

        seckillSkuRelationService.saveBatchAndCheckIfRedisNeedesToRefresh(seckillSkuRelationList);

        return R.ok();
    }

    @PostMapping("/update")
    @ApiOperation("根据id更新SeckillSkuRelation")
    public R update(@RequestBody SeckillSkuRelationEntity seckillSkuRelation) {
        seckillSkuRelationService.updateByIdAndCheckIfRedisNeedesToRefresh(seckillSkuRelation);

        return R.ok();
    }

    @PutMapping("/deleteBatch")
    @ApiOperation("根据id批量删除SeckillSkuRelation")
    public R delete(@RequestBody Long[] relationIds) {
        //连同删除缓存
        seckillSkuRelationService.removeByIdsAndInvalidCache(Arrays.asList(relationIds));

        return R.ok();
    }

}
